//
//  CoreDataStack.m
//  DogWalk
//
//  Created by Mac on 15/11/15.
//  Copyright © 2015年 riverlj. All rights reserved.
//

#import "CoreDataStack.h"

@interface CoreDataStack()
@property (nonatomic, strong, readwrite)NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong)NSURL *modelURL;
@property (nonatomic, strong)NSURL *storeURL;

@end

@implementation CoreDataStack
- (id)initWithStoreURL:(NSURL *)storeURL modelURL:(NSURL *)modelURL
{
    self = [super init];
    if (self) {
        self.storeURL = storeURL;
        self.modelURL = modelURL;
        [self setupManagedContenxt];
    }
    
    return self;
}

- (NSManagedObjectModel *)managedObjectModel{
    return [[NSManagedObjectModel alloc]initWithContentsOfURL:self.modelURL];
}

- (void)setupManagedContenxt {
    self.managedObjectContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
    self.managedObjectContext.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:self.managedObjectModel];
    
    NSError *error;
    [self.managedObjectContext.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.storeURL options:nil error:&error];
    if (error) {
        NSLog(@"error :%@",error);
    }
    
    self.managedObjectContext.undoManager = [[NSUndoManager alloc]init];
}


@end
